package com.jjpro.utils;
import com.alibaba.fastjson.JSONObject;
import com.jjpro.enums.ResultEnums;
import com.jjpro.exception.SiteException;
import jiujiu.Head.ApiHead;
import jiujiu.Verification.ApiVerification;


import javax.servlet.http.HttpServletRequest;

/**
 * 描述:
 * ----api接口管理类
 *
 * @outhor 张工
 * @create 2018-03-19 21:24
 */
public class ApiManager {
    private static final ThreadLocal<ApiHead> threadLocalHead=new ThreadLocal<>();

    private final static String APP_KEY="402880496058fbb7016058fc201e0009";

    public static ApiHead getApiHead(HttpServletRequest request){

//        ApiHead requestHead=getApiHead();
//        if(requestHead!=null){
//            return requestHead;
//        }else{
//            requestHead=new ApiHead();
//        }
        ApiHead requestHead=new ApiHead();
        requestHead.setVersion(request.getHeader("version"));
        requestHead.setCertType(request.getHeader("certType"));
        requestHead.setCertification(request.getHeader("certification"));
        requestHead.setTimestamp(request.getHeader("timestamp"));
        requestHead.setDeviceToken(request.getHeader("deviceToken"));
        requestHead.setUserId(request.getHeader("userId"));
        requestHead.setPlat(request.getHeader("plat"));
        requestHead.setOsInformation(request.getHeader("osInformation"));
        requestHead.setChannel(request.getHeader("channel"));
        requestHead.setLongitude(request.getHeader("longitude "));
        requestHead.setLatitude(request.getHeader("latitude "));
        requestHead.setIsTest(request.getIntHeader("isTest"));
        requestHead.setAppVersion(request.getHeader("appVersion"));
//        threadLocalHead.set(requestHead);
        return requestHead;
    }

    public static ApiHead getApiHead(){
        return threadLocalHead.get();
    }


    public static void valideHeadApi(ApiHead head) throws Exception {
        String valideMsg= ApiVerification.VerificationHandle(head,APP_KEY);
        JSONObject jsonObject=JSONObject.parseObject(valideMsg);
        //参数格式校验失败

        if(jsonObject.getInteger("code")==201){
            SiteException siteException=new
                    SiteException(ResultEnums.RETURN_PARAM_LOST);
            throw siteException;

        }else if(jsonObject.getInteger("code")==202){
            //签名校验失败
            SiteException siteException=new
                    SiteException(ResultEnums.RETURN_PARAM_SIGN);
            throw siteException;
        }
    }
}
